package com.lb.jwitter.action;


import java.text.DateFormat; 
import java.text.SimpleDateFormat; 
import java.util.ArrayList; 
import java.util.List;
import java.util.Map; 
 import com.opensymphony.xwork2.ActionContext; 
import java.util.Collection; 
import com.lb.jwitter.util.ApplicationContextProvider; 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; 
import org.springframework.test.context.ContextConfiguration;
import java.util.Date; 
import java.util.GregorianCalendar; 
import javax.xml.datatype.DatatypeFactory; 
import javax.xml.datatype.XMLGregorianCalendar; 
import org.springframework.context.support.ClassPathXmlApplicationContext; 
import com.lb.jwitter.service.ConditionReport; 
import com.lb.jwitter.util.Message; 
import com.opensymphony.xwork2.Action; 
import com.lb.jwitter.service.TblQuyenChiTietService;import com.lb.jwitter.service.TblTaiKhoan;import com.lb.jwitter.service.TblCTDTMonHocKem;
import com.lb.jwitter.service.TblCTDTMonHocKemService;
import com.lb.jwitter.service.TblCTDTMonHoc;
import com.lb.jwitter.service.TblCTDTMonHocService;

import com.lb.jwitter.service.TblCTDTMonHoc;
import com.lb.jwitter.service.TblCTDTMonHocService;

 /** 
 * @author QUOCVIET  
 * 
 */ 


@ContextConfiguration(locations="classpath:beans.xml") 
public class TblCTDTMonHocKemUpdateAction{
    private int pageSize; 
    public int getPageSize() {
       return pageSize;
 }

    public void setPageSize(int pageSize) { this.pageSize = pageSize; }

    private int page; 
    public int getPage() { return page; }

    public void setPage(int page) { this.page = page; }

    private int pages; 
    public int getPages() { return pages; }

    public void setPages(int pages) { this.pages = pages; }

    private String dataString; 
    public String getDataString() { return dataString; }

    private Message msg; 
    public Message getMsg() { return msg; }

    private int id; 
    public int getId() {
       return id;
 }

    public void setId(int id) {
       this.id = id;
 }

    private Long monHocId; 
    public void setMonHocId(Long monHocId) {
       this.monHocId = monHocId;
 }

    public Long getMonHocId() {
       return monHocId;
 }

    private TblCTDTMonHocService tblCTDTMonHocService; 

    private Long monHocKemId; 
    public void setMonHocKemId(Long monHocKemId) {
       this.monHocKemId = monHocKemId;
 }

    public Long getMonHocKemId() {
       return monHocKemId;
 }
    
    private String maMonHoc;
    public String getMaMonHoc() {
		return maMonHoc;
	}

	public void setMaMonHoc(String maMonHoc) {
		this.maMonHoc = maMonHoc;
	}

	public String getMaMonHocKem() {
		return maMonHocKem;
	}

	public void setMaMonHocKem(String maMonHocKem) {
		this.maMonHocKem = maMonHocKem;
	}

	private String maMonHocKem;
 

    private TblCTDTMonHocKem tblCTDTMonHocKem; 
    public void setTblCTDTMonHocKem (TblCTDTMonHocKem tblCTDTMonHocKem) {
        this.tblCTDTMonHocKem = tblCTDTMonHocKem;
 }

    private TblCTDTMonHocKemService tblCTDTMonHocKemService; 

	private int preUpdate(Message msg, TblCTDTMonHocKem obj ){ 
        msg.setPreMessage_b(true); 
        msg.setPreMessage_s("success"); 
        return 1; 
    } 
	private int postUpdate(Message msg, TblCTDTMonHocKem obj ){ 
        msg.setPostMessage_b(true); 
        msg.setPostMessage_s("success"); 
        return 1; 
    } 
  private TblTaiKhoan taiKhoanDangNhap; 
 public TblTaiKhoan getTaiKhoanDangNhap() { 
     return taiKhoanDangNhap; 
 } 
 public void setTaiKhoanDangNhap(TblTaiKhoan taiKhoanDangNhap) { 
     this.taiKhoanDangNhap = taiKhoanDangNhap; 
 } 
 private TblQuyenChiTietService tblQuyenChiTietService;
	 @Autowired // Injected by Spring 
     ApplicationContextProvider contextProvider; 
 public String execute() throws Exception 
{ 
 try{ 	
     Map session = ActionContext.getContext().getSession();
     taiKhoanDangNhap =  (TblTaiKhoan) session.get("taiKhoan");
     if( taiKhoanDangNhap == null) return "noprivilege";
    ApplicationContext context = ApplicationContextProvider.getApplicationContext(); 
    tblQuyenChiTietService = (TblQuyenChiTietService) context.getBean("tbl_Quyen_ChiTietClient"); 
    if(!tblQuyenChiTietService.checkRight(taiKhoanDangNhap.getMaNhomQuyen().getId(), "tbl_CTDT_MonHocKem", "sua")){ 
        return "noprivilege"; 
    } 
        tblCTDTMonHocKemService = (TblCTDTMonHocKemService) context.getBean("tbl_CTDT_MonHocKemClient"); 
        tblCTDTMonHocService = (TblCTDTMonHocService) context.getBean("tbl_CTDT_MonHocClient"); 

        tblCTDTMonHocService = (TblCTDTMonHocService) context.getBean("tbl_CTDT_MonHocClient"); 
        
     // tim id cho mso mon hoc
		List<ConditionReport> cons = new ArrayList<ConditionReport>();
		{
			ConditionReport con = new ConditionReport();
			con.setCol("ma");
			con.setValue1(maMonHoc);
			con.setOperator("eq");
			cons.add(con);
		}

		List<TblCTDTMonHoc> monhocs = tblCTDTMonHocService.findtblCTDTMonHocByCondition(cons, 0, 0);
		if (monhocs != null && monhocs.size() > 0) {
			monHocId = monhocs.get(0).getId();
		} else {
			return Action.ERROR;
		}
		
		cons.clear();
		{
			ConditionReport con = new ConditionReport();
			con.setCol("ma");
			con.setValue1(maMonHocKem);
			con.setOperator("eq");
			cons.add(con);
		}

		monhocs = tblCTDTMonHocService.findtblCTDTMonHocByCondition(cons, 0, 0);
		if (monhocs != null && monhocs.size() > 0) {
			monHocKemId = monhocs.get(0).getId();
		} else {
			return Action.ERROR;
		}

        msg = new Message(); 
        tblCTDTMonHocKem = tblCTDTMonHocKemService.gettblCTDTMonHocKem(new Long(id)); 
        tblCTDTMonHocKem.setMonHocId(tblCTDTMonHocService.gettblCTDTMonHoc(new Long(monHocId))); 
        tblCTDTMonHocKem.setMonHocKemId(tblCTDTMonHocService.gettblCTDTMonHoc(new Long(monHocKemId))); 
        preUpdate(msg,tblCTDTMonHocKem); 
       if (msg.isPreMessage_b()) 
       { 
           tblCTDTMonHocKemService.updatetblCTDTMonHocKem(tblCTDTMonHocKem); 
           postUpdate(msg, tblCTDTMonHocKem); 
           if (msg.isPostMessage_b() == false) 
           { 
               return Action.ERROR; 
           } 
       }else{ 
           return Action.ERROR; 
       } 

        return Action.SUCCESS; 
      } 
      catch(Exception e){ 
         System.out.println("Loi Update tbl_CTDT_MonHocKem Action"); 
         e.printStackTrace(); 
         return Action.ERROR; 
 } 
 } 
 } 
 
